<template>
  <div>
    <el-dialog :title="change.show?'修改商品类型':'新增商品类型'" :visible="visible" @close="closeFn" width="40%">
      <el-form :rules="rules" ref="from" :model="bear">
        <el-form-item label="商品类型名称:" prop="name">
          <el-input v-model="bear.className"  maxlength="10" show-word-limit autocomplete="off" placeholder="请输入内容"  style="width:80%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click.native="abst">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, revise } from '@/api/commodity/Type'
export default {
  watch: {
    change: {
      deep: true,
      handler () {
        if (this.change) {
          this.bear.className = this.change.className
          this.bear.classId = this.change.classId
        }
      }
    }
  },
  data () {
    return {
      bear: {
        classId: '',
        className: ''
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    change: {
      type: Object,
      required: true
    }
  },
  methods: {
    closeFn () {
      this.bear.className = ''
      this.$refs.from.resetFields()
      this.$emit('abst')
      this.$emit('update:visible', false)
    },
    async abst () {
      if (this.change.show) {
        const reg = await revise(this.bear)
        console.log(reg)
      } else {
        const res = await add(this.bear)
        console.log(res)
      }
      this.bear.className = ''
      this.bear.classId = ''
      this.$emit('abst')
      this.$refs.from.resetFields()
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
